package com.rfid.afd.activity.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.rfid.afd.activity.listener.CheckRfidListener;
import com.rfid.db.bean.Rfid;
import com.rfid.rls_psw.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DeleteAdapter extends RecyclerView.Adapter<DeleteAdapter.MyViewHolder> {

    private Context mContext;
    private List<Rfid> mRfids;
    private CheckRfidListener mCheckRfidListener;
    public DeleteAdapter(Context context, List<Rfid> data) {
        mContext = context;
        mRfids = data;
    }

    public void setCheckRfidListener(CheckRfidListener listener) {
        mCheckRfidListener = listener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.check_item_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Rfid rfid = mRfids.get(position);
        holder.name.setText(mRfids.get(position).getBookName());
        holder.barcode.setText(mRfids.get(position).getBarcode());
        holder.index.setText(mRfids.get(position).getRindex());
        holder.shelf.setText(mRfids.get(position).getShelf());
        holder.checkBox.setTag(rfid);
        holder.checkBox.setChecked(rfid.getChecked());
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Rfid rfid1 = (Rfid) buttonView.getTag();
                boolean check = rfid1.getChecked();
                rfid1.setChecked(isChecked);
                if(check != isChecked) {
                    if(mCheckRfidListener != null) {
                        mCheckRfidListener.onChecked(isChecked,rfid1);
                    }
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("toggle");
                holder.checkBox.toggle();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mRfids.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.book_name)
        TextView name;
        @BindView(R.id.barcode)
        TextView barcode;
        @BindView(R.id.index)
        TextView index;
        @BindView(R.id.shelf)
        TextView shelf;
        @BindView(R.id.checkbox)
        CheckBox checkBox;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
